/******************************************************************************* 
 * Copyright (C) 2012-2015 Microfountain Technology, Inc. All Rights Reserved. 
 * 
 * Unauthorized copying of this file, via any medium is strictly prohibited.   
 * Proprietary and confidential
 * 
 * Last Modified: 2015-11-12 18:55:04
 ******************************************************************************/

package cn.com.xy.sms.sdk.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 企业签名提取工具
 * @author javiylee
 */
public class CorpSignUtils {

    /**
     * 无用的签名信息
     */
    static String corpOutRegex = "(?:(?:http|https|ftp)://)?(?:[a-zA-Z0-9]{1,30})(?:\\.[a-zA-Z0-9]{1,30}){1,4}(?:[/?][^\\s]+)?|谨防|诈骗|(?:温馨|特别)?提[醒示]|泄露|回复|屏蔽|拨打|[致速]电|呼叫|请勿|勿向|注意";

    static Pattern corpOutPa = Pattern.compile(corpOutRegex);

    /**
     * 企业签名的英文tag
     */
    static String signTagFilter = "([\\[〔])|([〕\\]])";

    static Pattern signTagPa = Pattern.compile(signTagFilter);

    public static String getCorp(String msg) {
        msg = filter(msg);
        int len = msg.length();
        int sidx = 0;
        int eidx = 0;
        char tag = msg.charAt(sidx);
        String sign = null;
        if ('【' == tag) {
            tag = '】';
            eidx = msg.indexOf(tag);
            if (eidx != -1) {
                sign = msg.substring(sidx + 1, eidx);
                boolean vd = validSign(sign);
                if (vd)
                    return sign;
            }
        }
        eidx = len - 1;
        tag = msg.charAt(eidx);
        String sign2 = null;
        if ('】' == tag) {
            tag = '【';
            sidx = msg.lastIndexOf(tag);
            if (sidx > -1) {
                sign2 = msg.substring(sidx + 1, eidx);
                boolean vd = validSign(sign2);
                if (vd)
                    return sign2;
            }
        }
        return null;
    }

    /**
     * 验证签名
     * @param sign
     *            待验证签名
     * @return 验证结果
     */
    private static boolean validSign(String sign) {
        return sign != null && sign.trim().length() > 0 && !corpOutPa.matcher(sign).find();
    }

    private static String filter(String msg) {
        Matcher m = signTagPa.matcher(msg);
        boolean finded = m.find();
        if (finded) {
            StringBuffer sb = new StringBuffer();
            do {
                String a = m.group(1);
                String b = m.group(2);
                if (a != null)
                    m.appendReplacement(sb, "【");
                if (b != null)
                    m.appendReplacement(sb, "】");
                finded = m.find();
            } while (finded);
            m.appendTail(sb);
            msg = sb.toString();
        }
        msg = msg.replaceAll("([:： ])[:： ]+", "$1");
        // msg =
        // msg.replaceAll("(用户名?|账[号户]|[编序代][号码]|电话|号码|识别码|"+vcRegex+"|[：:])\\s*【",
        // "$1:");
        msg = msg.replaceAll("([,，。；！!;\\?][^【,，。；！!;\\?]*)【(?=[^】]*[,，。；！!;\\?])[^】]+】", "$1:");
        msg = msg.replaceFirst("[\\(（【]\\d/\\d[\\)）】]", "");
        return msg;
    }
}